package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.modbus.Modbus4jWriteUtils;
import com.ruoyi.system.domain.MrBed;
import com.ruoyi.system.domain.MrDeviceProtocolConfiguration;
import com.ruoyi.system.domain.MrWard;
import com.ruoyi.system.mapper.MrDeviceProtocolConfigurationMapper;
import com.ruoyi.system.service.IMrBedService;
import com.ruoyi.system.service.IMrDeviceProtocolConfigurationService;
import com.ruoyi.system.service.IMrWardService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 设备协议配置Service业务层处理
 *
 * @author lsw
 * @date 2024-01-03
 */
@Service
public class MrDeviceProtocolConfigurationServiceImpl implements IMrDeviceProtocolConfigurationService {
    @Autowired
    private MrDeviceProtocolConfigurationMapper mrDeviceProtocolConfigurationMapper;

    @Autowired
    private IMrWardService mrWardService;

    @Autowired
    private IMrBedService mrBedService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询设备协议配置
     *
     * @param id 设备协议配置主键
     * @return 设备协议配置
     */
    @Override
    public MrDeviceProtocolConfiguration selectMrDeviceProtocolConfigurationById(Long id) {
        return mrDeviceProtocolConfigurationMapper.selectMrDeviceProtocolConfigurationById(id);
    }

    /**
     * 查询设备协议配置列表
     *
     * @param mrDeviceProtocolConfiguration 设备协议配置
     * @return 设备协议配置
     */
    @Override
    public List<MrDeviceProtocolConfiguration> selectMrDeviceProtocolConfigurationList(MrDeviceProtocolConfiguration mrDeviceProtocolConfiguration) {
        return mrDeviceProtocolConfigurationMapper.selectMrDeviceProtocolConfigurationList(mrDeviceProtocolConfiguration);
    }

    /**
     * 新增设备协议配置
     *
     * @param mrDeviceProtocolConfiguration 设备协议配置
     * @return 结果
     */
    @Override
    public int insertMrDeviceProtocolConfiguration(MrDeviceProtocolConfiguration mrDeviceProtocolConfiguration) {
        return mrDeviceProtocolConfigurationMapper.insertMrDeviceProtocolConfiguration(mrDeviceProtocolConfiguration);
    }

    /**
     * 修改设备协议配置
     *
     * @param mrDeviceProtocolConfiguration 设备协议配置
     * @return 结果
     */
    @Override
    public int updateMrDeviceProtocolConfiguration(MrDeviceProtocolConfiguration mrDeviceProtocolConfiguration) {
        int count = mrDeviceProtocolConfigurationMapper.updateMrDeviceProtocolConfiguration(mrDeviceProtocolConfiguration);
        if (count > 0) {
            MrBed mrBed = new MrBed();
            mrBed.setGatewayProtocol(mrDeviceProtocolConfiguration.getId() + ",");
            List<MrBed> mrBeds = mrBedService.selectMrBedList(mrBed);
            if (!mrBeds.isEmpty()) {
                // 处理可能的 null 值（根据业务需求调整）
                Set<Long> distinctWardIds = mrBeds.stream()
                        .filter(bed -> bed != null && bed.getWardId() != null) // 过滤空对象和空 wardId
                        .map(MrBed::getWardId)
                        .collect(Collectors.toSet()); // 保留顺序的 Set

                for (Long wardId:distinctWardIds) {
                    MrWard mrWard = mrWardService.selectMrWardByWardId(wardId);
                    if (mrWard != null && !mrWard.getFloor().isEmpty()) {
                        String port = configService.selectConfigByKey(Constants.CONFIGURATION_PORT);
                        port = port == null ? "9999" : port;
                        try {
                            Modbus4jWriteUtils.writeCoil(mrWard.getFloor(), Integer.parseInt(port), 1, 1, true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        return count;
    }

    /**
     * 批量删除设备协议配置
     *
     * @param ids 需要删除的设备协议配置主键
     * @return 结果
     */
    @Override
    public int deleteMrDeviceProtocolConfigurationByIds(Long[] ids) {
        return mrDeviceProtocolConfigurationMapper.deleteMrDeviceProtocolConfigurationByIds(ids);
    }

    /**
     * 删除设备协议配置信息
     *
     * @param id 设备协议配置主键
     * @return 结果
     */
    @Override
    public int deleteMrDeviceProtocolConfigurationById(Long id) {
        return mrDeviceProtocolConfigurationMapper.deleteMrDeviceProtocolConfigurationById(id);
    }

    /**
     * 查询设备协议配置
     *
     * @param ids 需要查询的主键集合
     * @return 设备协议配置集合
     */
    @Override
    public List<MrDeviceProtocolConfiguration> selectMrDeviceProtocolConfigurationByIds(String[] ids) {
        return mrDeviceProtocolConfigurationMapper.selectMrDeviceProtocolConfigurationByIds(ids);
    }
}
